package com.atguigu.tingshu.search.receiver;

import com.atguigu.tingshu.common.rabbit.constant.MqConst;
import com.atguigu.tingshu.search.service.SearchService;
import com.rabbitmq.client.Channel;
import lombok.SneakyThrows;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class SearchReceiver {

    @Autowired
    private SearchService searchService;


    /**
     * 专辑上架
     *
     * @param albumId
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings=@QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_ALBUM_UPPER,durable = "true"),
            key = MqConst.ROUTING_ALBUM_UPPER
    ))
    public void upperAlbum(Long albumId, Message message, Channel channel) {
        //业务处理
        if(null != albumId){
            searchService.upperAlbum(albumId);
        }

        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }

    /**
     * 专辑下架
     *
     * @param albumId
     * @param message
     * @param channel
     */
    @SneakyThrows
    @RabbitListener(bindings=@QueueBinding(
            exchange = @Exchange(value = MqConst.EXCHANGE_ALBUM,durable = "true"),
            value = @Queue(value = MqConst.QUEUE_ALBUM_LOWER,durable = "true"),
            key = MqConst.ROUTING_ALBUM_LOWER
    ))
    public void lowerAlbum(Long albumId, Message message, Channel channel) {
        //业务处理
        if(null != albumId){
            searchService.lowerAlbum(albumId);
        }
        //手动应答
        channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
    }
}
